// @Auth:zdl
package bll

import (
	"encoding/json"
	"fmt"
	"net/url"

	"gitee.com/sky_big/mylog"
	"github.com/gogf/gf/g/net/ghttp"

	. "github.com/zhongdalu/DoorValveErWang/model"
	"github.com/zhongdalu/DoorValveErWang/public"
	. "github.com/zhongdalu/DoorValveErWang/record"
	"github.com/zhongdalu/DoorValveErWang/util"
)

func getApiData() (*DoorInfo, error) {
	bs, err := util.HttpGet(public.HttpUrl + "/hf_interface.ashx?method=gethfInfo")
	if err != nil {
		mylog.Error(err)
		return nil, err
	}
	var data DoorInfo
	err = json.Unmarshal(bs, &data)
	if err != nil {
		mylog.Error(err)
		return nil, err
	} else {
		mylog.Println(fmt.Sprintf("?method=gethfInfo=>%+v", data))
	}
	return &data, err
}

func records(crd *CmdRecord) error {
	step, allNum, finNum, sucNum := crd.GetStep(), crd.GetAllNum(), houseFac.GetFinishNum(), houseFac.GetSuccessNum()
	urls := public.HttpUrl + fmt.Sprintf("/hf_interface.ashx?method=getzhixing_pingheng&step=%d&allNums=%d&finshedNums=%d&successNums=%d&pinghengNum=%s", step, allNum, finNum, sucNum, Crd.GetClassId())
	_, err := util.HttpGet(urls)
	if err != nil {
		mylog.Error(err)
		return err
	} else {
		mylog.Println("发送成功:", urls)
	}
	return nil
}

func sendOperaResultInfo(info OperaResultInfo) {
	bs, err := json.Marshal(info)
	if err != nil {
		mylog.Error(err)
		return
	}
	var c = ghttp.NewClient()
	c.SetHeader("Content-Type", "application/x-www-form-urlencoded")
	dataUrlVal := url.Values{}
	dataUrlVal.Add("passStr", string(bs))
	resp, err := c.Post(public.HttpUrl+"/hf_interface.ashx?method=pinghengWrite_f_rizhi", dataUrlVal.Encode())
	if err != nil {
		mylog.Error(err)
		return
	} else {
		mylog.Println("pinghengWrite_f_rizhi=>", resp.ReadAllString(), fmt.Sprintf("%+v", dataUrlVal))
	}
	defer func() {
		_ = resp.Close()
	}()
	if resp.StatusCode != 200 {
		mylog.Error(resp.Status)
	}
}
